package android.reavlviewdemo;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;

/**
 * @author liuml
 * @explain
 * @time 2018/2/3 15:44
 */

public class RevealDrawable extends Drawable {

    //Drawable 需要实现setBounds

    //灰色部分
    private Drawable mUnselectDrawable;
    //彩色部分
    private Drawable mSelectDrawable;

    private Rect mTempRect = new Rect();

    public RevealDrawable(Drawable mUnselectDrawable, Drawable mSelectDrawable) {
        this.mSelectDrawable = mSelectDrawable;
        this.mUnselectDrawable = mUnselectDrawable;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {


        //将两张图片进行裁剪和拼接
        int level = getLevel();
        if (level == 0 || level == 10000) {
            // 画整张灰色的图
            mUnselectDrawable.draw(canvas);
        } else if (level == 5000) {
            // 画整张彩色的图
            mSelectDrawable.draw(canvas);
        } else {

            //获取边界矩形
            Rect bounds = getBounds();
            //初始化Rect
            Rect r = mTempRect;

            // 比例 -1 ~ 1 之间进行变化 -1 ~0 表示左边灰色，右边彩色
            // 0 ~1 表示左边是彩色，右边灰色
            float ratio = (level / 5000f) - 1f;


            //1. 画出灰色区域
            {
                //获取边界矩阵的宽高
                int w = bounds.width();
                int h = bounds.height();
                //判断从哪个方向进行裁剪 可以看下图 当0-5000时左边是灰色右边是彩色, 当5000-10000时 左边是彩色右边是灰色
                int gravity = ratio < 0 ? Gravity.LEFT : Gravity.RIGHT;

                //目标裁剪的宽度 动态计算
                w = (int) (w * Math.abs(ratio));
                //从一个矩形区域裁剪出目标矩形
                Gravity.apply(
                        gravity,// 从哪个方向开始剪，左边还是右边
                        w,// 目标矩形的宽
                        h, // 目标矩形的高
                        bounds,// 被剪裁图片的rect
                        r// 目标rect
                );
                canvas.save();// 因为下面需要对canvas进行裁剪 先保存一次canvas,后面进行还原.
                canvas.clipRect(r);//裁剪目标rect
                //画出
                mUnselectDrawable.draw(canvas);

                canvas.restore();//还原canvas 方便下一次进行裁剪.
            }

            //2. 画出才彩色区域
            {
                //获取边界矩阵的宽高
                int w = bounds.width();
                int h = bounds.height();
                int gravity = ratio < 0 ? Gravity.RIGHT : Gravity.LEFT;
                //目标裁剪的宽度 动态计算  这里是剩下的距离
                w -= (int) (w * Math.abs(ratio));
                Gravity.apply(
                        gravity,
                        w,
                        h,
                        bounds,
                        r
                );
                canvas.save();
                canvas.clipRect(r);
                mSelectDrawable.draw(canvas);
                canvas.restore();
            }
        }


    }


    @Override
    protected boolean onLevelChange(int level) {
        // 当设置level时，来重绘Drawable
        invalidateSelf();
        return super.onLevelChange(level);
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        //定义两张图片的宽高
        mSelectDrawable.setBounds(bounds);
        mUnselectDrawable.setBounds(bounds);
    }

    @Override
    public int getIntrinsicHeight() {//返回可绘制的固有高度
        return Math.max(mSelectDrawable.getIntrinsicHeight(), mUnselectDrawable.getIntrinsicHeight());
    }

    @Override
    public int getIntrinsicWidth() {//返回可绘制的固有宽度
        return Math.max(mSelectDrawable.getIntrinsicWidth(), mUnselectDrawable.getIntrinsicWidth());
    }

    @Override
    public void setAlpha(int alpha) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }
}
